package com.wlyy.bcwlw.common.export2excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.wlyy.bcwlw.common.export2excel.entity.Export2ExcelColumn;
import com.wlyy.bcwlw.common.export2excel.entity.Export2ExcelHeader;
import com.wlyy.bcwlw.common.export2excel.entity.Export2ExcelRow;
import com.wlyy.bcwlw.common.export2excel.exception.ExportNoHeaderException;
import com.wlyy.bcwlw.common.export2excel.exception.ExportNoRowException;
import com.wlyy.bcwlw.common.export2excel.exception.ImpExpException;
import com.wlyy.bcwlw.common.export2excel.impl.ExcelExportImpl;
import com.wlyy.bcwlw.common.utils.ExcelUtil;
/**
 * 
 * @ClassName. WebExport2ExcelHandler
 * @Description. web页面导出excel处理
 * @author zhangyang
 * @date 2016年5月31日 上午8:59:45
 * @version V1.0
 */

public class WebExport2ExcelHandler {

	private static final Logger log = LogManager.getLogger("dev_log");

	private Export2ExcelRow[] rows;

	private String[] header;

	public WebExport2ExcelHandler(String[] header, Export2ExcelRow[] rows) {
		this.rows = rows;
		this.header = header;
	}

	public void export2Excel(HttpServletResponse response) throws ImpExpException, IOException {
		this.export2Excel(response, header, rows);
	}

	public static void export2Excel(HttpServletResponse response,
			String[] header, Export2ExcelRow[] exportRows)
			throws ImpExpException, IOException {
		HSSFWorkbook wb = constructExportCell(header, exportRows);
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition",
				"attachment;filename=export.xls");
		OutputStream output = response.getOutputStream();
		ExcelUtil.exportWorkBook(wb, output);
	}

	public static void export2Excel(HttpServletResponse response,
			List<Export2ExcelHeader> header, Export2ExcelRow[] exportRows)
			throws ImpExpException, IOException {
		HSSFWorkbook wb = constructExportCell(header, exportRows);
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition",
				"attachment;filename=export.xls");
		OutputStream output = response.getOutputStream();
		ExcelUtil.exportWorkBook(wb, output);
	}

	private static HSSFWorkbook constructExportCell(String[] headers,
			Export2ExcelRow[] exportRows) throws ImpExpException {
		List<Export2ExcelHeader> excelHeader = new ArrayList<Export2ExcelHeader>();
		if (headers != null) {
			for (String header : headers) {
				excelHeader.add(new Export2ExcelHeader(header));
			}
		}
		return constructExportCell(excelHeader, exportRows);
	}

	private static HSSFWorkbook constructExportCell(
			List<Export2ExcelHeader> header, Export2ExcelRow[] exportRows)
			throws ImpExpException {
		if (header.isEmpty()) {
			throw new ExportNoHeaderException();
		}
		if (ArrayUtils.isEmpty(exportRows) && log.isDebugEnabled()) {
			log.debug("export2Excel Exception", new ExportNoRowException());
		}
		List<Export2ExcelRow> data;
		if (ArrayUtils.isEmpty(exportRows)) {
			data = new ArrayList<Export2ExcelRow>();
			List<Export2ExcelColumn> emptyColumns = new ArrayList<Export2ExcelColumn>();
			for (int i = 0; i < header.size(); i++) {
				emptyColumns.add(new Export2ExcelColumn("", ""));
			}
			data.add(new Export2ExcelRow(emptyColumns
					.toArray(new Export2ExcelColumn[0])));
		} else {
			data = Arrays.asList(exportRows);
		}
		return new ExcelExportImpl().export(
				header.toArray(new Export2ExcelHeader[0]), data, null);
	}

	/**
	 * 导出路径需要再修改
	 */
	public static void export2Excel(String fileName, String[] header,
			Export2ExcelRow[] exportRows) throws ImpExpException, IOException {
		HSSFWorkbook wb = constructExportCell(header, exportRows);
		File excelFile =new File("D:/temp_excel/" + fileName+".xls");
		OutputStream output = new FileOutputStream(excelFile);
		try {
			ExcelUtil.exportWorkBook(wb, output);
		} finally {
			if (output != null) {
				output.close();
			}
		}
	}

	public Export2ExcelRow[] getRows() {
		return rows;
	}

	public void setRows(Export2ExcelRow[] rows) {
		this.rows = rows;
	}
}
